﻿/**
 * Copyright (C) 2004-2009 D3Systems, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace OMGServer
{
    /// <summary>
    /// Service class of the OMG Service.
    /// </summary>
    public partial class OmgService : ServiceBase
    {
        // The Manager object in charge of receiving and processing incoming connections
        /// <summary>
        /// Reference to the ConnectionMgr object in charge of handling incoming connections.
        /// </summary>
        ConnectionMgr conMgr;

        OMGGateways.XMPPGateway xmpp;

        /// <summary>
        /// Constructor for the Service
        /// </summary>
        public OmgService()
        {
            // By Default, Initialize the components ...
            InitializeComponent();

            // Initialize and create the Event Logger if it has not yet been initialized ... 
            if (!System.Diagnostics.EventLog.SourceExists("ServiceLogs"))
            {
                System.Diagnostics.EventLog.CreateEventSource("ServiceLogs", "OMGLogs");                
            }
            eventLog1.Source = "ServiceLogs";
            eventLog1.Log = "OMGLogs";
        }

        /// <summary>
        /// Called when the service has been started.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("Starting Connection Manager!");

            try
            {
                conMgr = new ConnectionMgr(eventLog1);
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("Error Starting Connection Manager: " + ex.Message);
            }

            
            OMGGateways.GWController.getInstance().SetLogger(eventLog1);
            
            OMGGateways.ConfigurationHandler.checkDir();
            OMGGateways.ConfigurationHandler.checkFile();
            eventLog1.WriteEntry("Reading Config File: " + OMGGateways.ConfigurationHandler.ReadConfigFile());

            OMGGateways.GWController.getInstance().LoginAll();
            
        }

        /// <summary>
        /// Called when the service has been stopped.
        /// </summary>
        protected override void OnStop()
        {
            eventLog1.WriteEntry("In OnStop");
            conMgr.isRunning = false;
 
        }

        /// <summary>
        /// Called when the service has been resumed.
        /// </summary>
        protected override void OnContinue()
        {
            eventLog1.WriteEntry("In OnContinue");
        }

    }
}
